package com.vesystem.vegua.modules.common.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.vesystem.vegua.modules.common.dto.SysUserDto;
import com.vesystem.vegua.modules.common.dto.UserSessionDto;
import com.vesystem.vegua.modules.common.entity.SysUser;
import com.vesystem.vegua.modules.common.service.impl.SysUserServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author hcy
 * @since 2020-12-08
 */
@RestController
@RequestMapping("/user")
public class SysUserController {

    @Autowired
    private SysUserServiceImpl sysUserService;

    @PostMapping("userLogin")
    public String userLogin(@RequestBody SysUserDto userDto){
        return sysUserService.userLogin(userDto.getLoginname(),userDto.getPassword());
    }

    @PostMapping("refreshToken")
    public String refreshToken(HttpServletRequest request){
        return sysUserService.refreshToken(request);
    }

    @PostMapping("userLogout")
    public void userLogout(HttpServletRequest request){
        sysUserService.userLogout(request);
    }

    @PostMapping("addUser")
    public void addUser(@RequestBody SysUserDto user){
        sysUserService.addUser(user);
    }

    @PutMapping("updateSelfPassword")
    public void updateSelfPassword(@RequestBody Map<String,String> map,HttpServletRequest request){
        sysUserService.updateSelfPassword(map.get("oldPassword"),map.get("newPassword"),request);
    }


    @PutMapping("updateUserPasswordById")
    public void updateUserPasswordById(@RequestBody SysUserDto user){
        sysUserService.updateUserPasswordById(user);
    }

    @PutMapping("updateUserStatusById")
    public void updateUserStatusById(@RequestBody SysUserDto user){
        sysUserService.updateUserStatusById(user);
    }

    @DeleteMapping("deleteUserById")
    public void deleteUserById(@RequestBody Map<String,List<Integer>> param){
        sysUserService.deleteUserById(param.get("userIds"));
    }

    @PutMapping("updateUserAliasById")
    public void updateUserAliasById(@RequestBody SysUserDto user){
        sysUserService.updateUserAliasById(user);
    }

    @GetMapping("adminGetUserList")
    public Page<SysUser> adminGetUserList(Page<SysUser> page, String alias, String loginname){
        return sysUserService.adminGetUserList(page,alias,loginname);
    }

    @GetMapping("getSysUserDetailById/{userId}")
    public SysUser getSysUserDetailById(@PathVariable Integer userId){
        return sysUserService.getSysUserDetailById(userId);
    }

    @GetMapping("getSessionList")
    public List<UserSessionDto> getSessionList(String loginName){
        return sysUserService.getSessionList(loginName);
    }

    @DeleteMapping("unsubscribeSession")
    public void unsubscribeSession(@RequestBody Map<String,String> map){
        sysUserService.unsubscribeSession(map.get("loginName"));
    }

    @DeleteMapping("blockedAccount")
    public void blockedAccount(@RequestBody Map<String,String> map){
        sysUserService.blockedAccount(map.get("loginName"));
    }




}

